package com.huawei.msw.ssm.service;

import com.huawei.msw.ssm.dao.mapper.DepartmentMapper;
import com.huawei.msw.ssm.model.Department;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

/**
 * Created by hadoop on 16-12-4.
 */
@Service
public class DepartmentService {

    private static final Logger LOG = LoggerFactory.getLogger(DepartmentService.class);

    @Resource
    private DepartmentMapper departmentMapper;

    public void saveDepartment(Department department) {
        int affected =  this.departmentMapper.saveDepartment(department);
        if (affected > 0) {
            LOG.info("save department to db success.");
        } else {
            LOG.warn("save department to db failure.");
        }
    }

    /**
     *
     * @return
     */
    private String selectPersonSql() {
        return new SQL() {{
            SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME");
            SELECT("P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON");
            FROM("PERSON P");
            FROM("ACCOUNT A");
            INNER_JOIN("DEPARTMENT D on D.ID = P.DEPARTMENT_ID");
            INNER_JOIN("COMPANY C on D.COMPANY_ID = C.ID");
            WHERE("P.ID = A.ID");
            WHERE("P.FIRST_NAME like ?");
            OR();
            WHERE("P.LAST_NAME like ?");
            GROUP_BY("P.ID");
            HAVING("P.LAST_NAME like ?");
            OR();
            HAVING("P.FIRST_NAME like ?");
            ORDER_BY("P.ID");
            ORDER_BY("P.FULL_NAME");
        }}.toString();
    }

}
